package com.baozun.houji.me.activity.address

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.model.bean.AddressInfo
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.EmptyView
import com.baozun.houji.me.R
import com.baozun.houji.me.adapter.AddressListAdapter
import com.baozun.houji.me.databinding.ActivityAddressListBinding
import com.baozun.houji.me.viewmodel.AddressListViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.hjq.bar.TitleBar
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration

@Route(path = RoutePath.Me.ADDRESS_LIST)
class AddressListActivity : BaseActivity<AddressListViewModel, ActivityAddressListBinding>(), OnItemClickListener, OnItemChildClickListener {

    private val mIsSelectAddress by lazy { intent.getBooleanExtra(Constants.Common.IS_SELECT_ADDRESS, false) }

    var adapter: AddressListAdapter? = null
    override fun initView(savedInstanceState: Bundle?) {
        adapter = AddressListAdapter(mutableListOf(), this)
        mDataBinding.addressListRv.adapter = adapter
        adapter?.setOnItemClickListener(this)
        adapter?.setEmptyView(
            EmptyView.Builder(this).emptyHintText(com.baozhun.mall.res.R.string.no_address_data_hint)
            .emptyImage(R.drawable.ic_data_empty_address)
            .build())
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getAddressList()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.create_address_tv -> {
                ARouter.getInstance().build(RoutePath.Me.MODIFY_ADDRESS).navigation()
            }
        }
    }

    override fun onLeftClick(titleBar: TitleBar?) {
        when (titleBar?.leftIcon2Action) {
            TitleBar.ACTION_ICON_CLICK_BACK -> {
                if (mViewModel.addressList.value?.size == 1) {
                    var intent = Intent().apply {
                        putExtra(Constants.Me.ADDRESS_INFO, mViewModel.addressList.value?.get(0))
                    }
                    setResult(Activity.RESULT_OK, intent)
                }
            }
        }
        super.onLeftClick(titleBar)
    }

    override fun createObserver() {
        mViewModel.addressList.observe(this) {
            adapter?.setList(it)
        }

        mViewModel.defaultResult.observe(this) {
            mViewModel.getAddressList()
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        var address = mViewModel.addressList.value?.get(position)
        if (mIsSelectAddress) {
            var intent = Intent().apply {
                putExtra(Constants.Me.ADDRESS_INFO, address)
            }
            setResult(Activity.RESULT_OK, intent)
            finish()
        } else {
            ARouter.getInstance().build(RoutePath.Me.MODIFY_ADDRESS)
                .withSerializable(
                    Constants.Common.BUNDLE_NAME,
                    address
                )
                .navigation()
        }

    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        mViewModel.addressList.value?.get(position)?.let { mViewModel.setDefaultAddress(it) }
    }
}